package contract.user.service;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.jws.WebService;

import user.entity.User;


/**
 * 基于契约的发布webservice,先编写wsdl,生成服务端代码
 * 生成的接口抛出的异常类，不可使用。要抛出自定义的异常类，才能被客户端捕获
 * @author ddf 2016年10月9日下午2:13:22
 *
 */
@WebService(endpointInterface="contract.user.service.ContractUserService")
public class ContractUserServiceImpl implements ContractUserService{
	private Map<String, User> userMap = new HashMap<String, User>();
	
	@Override
	public void addUser(User user) throws contract.user.service.exception.RepetUserException {
		// throw new contract.user.service.exception.RepetUserException("用户已经存在");
		throw new RuntimeException("用户已经存在！");
		/*if(userMap.get(user.getUsername()) == null) {
			userMap.put(user.getUsername(), user);
		} else {
			File file = new File("C:" + File.separator + "LOG1" + File.separator);
			if(!file.exists()) {
				file.mkdirs();
			}
			FileWriter fw = null;
			try {
				File log = new File(file + File.separator + "a.txt");
				if(!log.exists()) {
					log.createNewFile();
				}
				fw = new FileWriter(log);
				fw.write(user.getUsername() + "已经存在！");
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} finally {
				try {
					fw.flush();
					fw.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			throw new contract.user.service.exception.RepetUserException("用户已经存在");
		}*/
	}

	@Override
	public void deleteUser(String username) {
		userMap.remove(username);
	}

	@Override
	public List<User> getAllUser() {
		Collection<User> userColl = userMap.values();
		Iterator<User> userIterator = userColl.iterator();
		List<User> userList = new ArrayList<User>();
		while(userIterator.hasNext()) {
			userList.add(userIterator.next());
		}
		return userList;
	}
}
